
layui.use(['jquery', 'laydate', 'febs', 'form', 'table', 'commonJS', 'validate', 'laydate'], function () {
    var $ = layui.jquery,
        laydate = layui.laydate,
        febs = layui.febs,
        form = layui.form,
        commonJS = layui.commonJS,
        validate = layui.validate,
        laydate = layui.laydate,
        $view = $('#febs-baseSafetyScoreConfig'),
        $query = $view.find('#query'),
        $searchForm = $view.find('#baseSafetyScoreConfig-table-form'),
        $tenantSelect = $searchForm.find('select[name="tenantSelect"]'),
        $dateDiv = $view.find('#dateDiv'),
        $resetDate = $view.find('#resetDate'),
        $settlementDate = $view.find('#settlementDate'),
        tableIns;

    laydate.render({
        elem: "#resetDate",
        type: "date",
        done: function (value, date) {
            let settlementDate = $settlementDate.val();
            if (settlementDate > value) {
                febs.alert.warn("重置日期不能小于结算日期")
                $resetDate.val('')
            }
        },
    })
    laydate.render({
        elem: "#settlementDate",
        type: "date",
        done: function (value, date) {
            let resetDate = $resetDate.val();
            if (value > resetDate) {
                febs.alert.warn("结算日期必须小于重置日期")
                $settlementDate.val('')
            }
        },
    })

    form.verify(validate);

    initFormValue();

    function initFormValue() {
        if (baseSafetyScoreConfig) {
            let scoreRestFlg = baseSafetyScoreConfig.scoreRestFlg;
            form.val("baseSafetyScoreConfig-form", {
                "safetyScoreConfigId": baseSafetyScoreConfig.safetyScoreConfigId,
                "tenantId": baseSafetyScoreConfig.tenantId,
                "scoreRestFlg": scoreRestFlg == 1 ? 'on' : '',
                "resetDate": baseSafetyScoreConfig.resetDate,
                "settlementDate": baseSafetyScoreConfig.settlementDate,
            });
            if (scoreRestFlg == 1) {
                $dateDiv.show();
            }else {
                $dateDiv.hide();
            }

        }
    }



    function reloadPage() {
        var tenantId=$tenantSelect.val();
        febs.get(ctx + 'baseSafetyScoreConfig/search/' + tenantId, null, function (data) {
            var newBaseSafetyScoreConfig = "";
            if(data.code==200&&data.data.baseSafetyScoreConfig){
                newBaseSafetyScoreConfig = data.data.baseSafetyScoreConfig;
            }
            let scoreRestFlg = newBaseSafetyScoreConfig.scoreRestFlg;
            form.val("baseSafetyScoreConfig-form", {
                "safetyScoreConfigId": newBaseSafetyScoreConfig.safetyScoreConfigId,
                "tenantId": newBaseSafetyScoreConfig.tenantId,
                "scoreRestFlg": scoreRestFlg == 1 ? 'on' : '',
                "resetDate": newBaseSafetyScoreConfig.resetDate,
                "settlementDate": newBaseSafetyScoreConfig.settlementDate,
            });
            if (scoreRestFlg == 1) {
                $dateDiv.show();
            }else {
                $dateDiv.hide();
            }

        })
    }

    $query.on('click', function () {
        reloadPage();
    });

    // 监听积分重置开关
    form.on('switch(scoreRestFlg)', function (data) {
        let status = data.elem.checked;
        if (status) {
            $dateDiv.show();
        }else {
            $dateDiv.hide();
            $resetDate.val('')
            $settlementDate.val('')
        }
    })

    form.on('submit(baseSafetyScoreConfig-form-submit)', function (data) {
        if(data.field.tenantId==null || data.field.tenantId ==''){
            data.field.tenantId = $tenantSelect.val();
        }
        let scoreRestFlg = data.field.scoreRestFlg;
        if (data.field.scoreRestFlg == 'on') {
            data.field.scoreRestFlg = 1
            if (!$resetDate.val()) {
                febs.alert.warn('重置日期不能为空');
                return false
            }
            if (!$settlementDate.val()) {
                febs.alert.warn('结算日期不能为空');
                return false
            }
        }else {
            data.field.scoreRestFlg = 0
        }
        febs.modal.confirm('保存提示', '是否保存配置信息？', function () {
            if (data.field.moduleConfigId && data.field.moduleConfigId.length > 0) {
                febs.postArray(ctx + 'baseSafetyScoreConfig/update', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                });
            } else {
                febs.postArray(ctx + 'baseSafetyScoreConfig', data.field, function () {
                    layer.closeAll();
                    febs.alert.success('保存成功');
                    reloadPage();
                });
            }
        });

        return false;
    });


    form.render();


});